<template>
  <div class="ask-list">
    <div class="search-modal">
      <search
        @result-click="resultClick"
        @on-change="getResult"
        :results="results"
        v-model="value"
        position="fixed"
        @on-focus="onFocus"
        @on-cancel="onCancel"
        @on-submit="onSubmit"
        ref="search"></search>
    </div>
    <scroller  lock-x scrollbar-y height="-40" use-pullup :pullup-config="pullupConfig2" ref="demo2" @on-pullup-loading="load2">
      <ul class="list">
        <li class="flex-box-align" v-for="(item, index) in dataList" :key="index">
          <img :src="item.patientUserCoreDTO.images[0].fileUrl" alt="" v-if="item.patientUserCoreDTO.images.length">
          <img src="" alt="" v-else>
          <span class="flex">{{item.patientUserCoreDTO.trueName}}</span>
          <span class="btn-record" @click="linkToAskRecordList(item.id)">问诊单记录</span>
        </li>
      </ul>
      <div v-if="noData" class="no-data">没有更多内容</div>
    </scroller>
  </div>
</template>
<script>
  import { Search, Scroller } from 'vux';
  let CONSTANT = require('../../../wechat/static/constant/constant.js'),
    common = require("../../../wechat/static/common/common.js"),
    wsService = require("../../../wechat/static/common/wechat-jsdk.js");

  export default {
    data() {
      return {
        results: [],
        value: "",
        dataList: [],
        pullupConfig2: {
          content: '上拉加载更多',
          downContent: '松开进行加载',
          upContent: '上拉加载更多',
          loadingContent: '加载中...'
        },
        pageIndex:1,
        dataForm:'',
        loading:true,
        noData:false
      }
    },
    components: {
      Search, Scroller
    },
    mounted() {
      this.getList()
    },
    methods: {
      load2 () {
        if(!this.loading) return false;
        let { pageIndex, pageCount} = this.dataForm;
        if (pageCount <= this.pageIndex){
          setTimeout(() => {
            this.$refs.demo2.disablePullup()
            this.noData = true
          }, 100)
          return false;
        }
        this.loading = false;
        this.pageIndex++;
        setTimeout(() => {
          this.getList()
          this.$refs.demo2.donePullup()
        }, 100)
      },
      getList() {
        let  url = CONSTANT.URL.PATIENT.PATIENTLIST,
          param = {pageIndex:this.pageIndex,pageSize:10}
        common.requestAjax(url,JSON.stringify({trueNameLike: "",queryType: ""}),param,(res)=>{
          if(res.status == 200){
            this.loading = true;
            let data = res.data,
              pageCount = data.pageCount,
              bussData = data.bussData
            if(pageCount == 0||pageCount == 1){
              this.$refs.demo2.disablePullup()
              this.noData = true
            }
            this.dataForm = data;
            this.dataList =  this.pageIndex <= pageCount ? this.dataList.concat(bussData) : this.dataList
          }else{

          }
        })
      },
      setFocus () {
        this.$refs.search.setFocus()
      },
      resultClick (item) {
        this.$router.push({path: "/ask/record/list", query: {id: item.id}})
      },
      getResult (val) {
        console.log('on-change', val)
        this.results = val ? this.getResult1(val) : []
      },
      getResult1(val) {
        let rs = []
        for (let i = 0; i < this.dataList.length; i++) {
          if (this.dataList[i].patientUserCoreDTO.trueName.indexOf(val) != -1) {

            rs.push({
              title: this.dataList[i].patientUserCoreDTO.trueName,
              id: this.dataList[i].patientId,
            })
          }
        }
        return rs
      },
      onSubmit () {
        this.$refs.search.setBlur()
        this.$vux.toast.show({
          type: 'text',
          position: 'top',
          text: 'on submit'
        })
      },
      onFocus () {
        console.log('on focus')
      },
      onCancel () {
        console.log('on cancel')
      },
      linkToAskRecordList(id) {
        this.$router.push({path: "/ask/record/list", query: {id: id}})
      }
    }
  };
</script>
<style lang="less">
  .xs-plugin-pullup-container{
    font-size: 12px;
    bottom: -50px;
  }
  .no-data{
    padding-top: .5rem;
    color: #999;
    font-size: 14px;
    bottom: 0rem;
    text-align: center;
    position: relative;
  }
  .ask-list {
    font-size: .34rem;
    .list {
      li {
        padding: .17rem 0.21rem .17rem 0;
        margin-left: .21rem;
        border-bottom: 1px solid #e7e7e7;
        img {
          width: .76rem;
          height: .76rem;
          vertical-align: middle;
        }
        span {
          margin-left: .28rem;
        }
        .btn-record {
          width: 2rem;
          height: .6rem;
          line-height: .6rem;
          text-align: center;
          background-color: #f8f8f8;
          border: 1px solid #dfdfdf;
          border-radius: .06rem;
          font-size: .26rem;
          color: #444;
        }
      }
    }
  }

  .search-modal {
    font-size: .36rem;
  }
</style>
